#include "etcd.hpp"
#include "channel.hpp"
#include "speech.pb.h"
#include "aip-cpp-sdk-4.16.7/speech.h"
int main()
{
    init_logger();
    ServiceManager service_manager;
    bool ret = service_manager.declared("/service/speech_service");
    if (!ret) {
        LOG_ERROR("服务声明失败");
        return -1;
    }
    auto put_cb = [&service_manager](const std::string& s1, const std::string& s2)->void
    {
        service_manager.onServiceOnline(s1, s2);
    };
    auto del_cb = [&service_manager](const std::string& s1, const std::string& s2)->void
    {
        service_manager.onServiceOffline(s1, s2);
    };
    Discovery discover("http://127.0.0.1:2379", "/service", put_cb, del_cb);
    auto ptr = service_manager.choose("/service/speech_service");
    if(ptr == nullptr)
    {
        LOG_ERROR("未找到可用信道");
        return -1;
    }
    auto channel = ptr->choose();
    if(channel == nullptr)
    {
        LOG_ERROR("未找到可用信道");
        return -1;
    }
    chat_system::SpeechService_Stub stub(channel.get());
    chat_system::SpeechRecognitionReq req;
    chat_system::SpeechRecognitionRsp rsp;

    std::string content;
    aip::get_file_content("16k.pcm", &content);
    LOG_DEBUG("读取音频文件成功, size: {}", content.size());
    req.set_speech_content(content);
    req.set_request_id("1");

    brpc::Controller cntl = brpc::Controller();
    stub.SpeechRecognition(&cntl, &req, &rsp, nullptr);
    if(cntl.Failed())
    {
        LOG_ERROR("RPC调用失败: {}", cntl.ErrorText());
        return -1;
    }
    if(!rsp.success())
    {
        LOG_ERROR("语音识别失败: {}", rsp.errmsg());
        return -1;
    }
    LOG_INFO("{} 同步调用完成: {}", rsp.request_id(), rsp.recognition_result());
    std::this_thread::sleep_for(std::chrono::seconds(10));
    return 0;
}